#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define high 19//全局变量
#define width 40//画面尺寸
#define enemy_number 5
int a[high][width] = { 0 };
int position_x, position_y;
int radius;
int v_x, v_y;
int ball_x, ball_y;
int sum;
int v;
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
void startup()//数据初始化
{
	int i, j;
	v_x = 1, v_y = 1;
	sum = 0;
	v = 10;
	for (i = 0; i < high; i++)
	{
		a[i][0] = 1;
		a[i][width - 1] = 1;
		for (j = 0; j < width; j++)
		{
			a[0][j] = 1;
			a[high - 1][j] = 1;
		}
	}
	for (i = 1; i < high - 14; i++)
	{
		for (j = 1; j < width - 1; j++)
		{
			a[i][j] = 4;
		}
	}
	position_x = high - 2;
	position_y = width / 2;
	radius = 3;
	for (j = position_y - radius; j <= position_y + radius; j++)
	{
		a[position_x][j] = 2;
	}
	ball_x = high / 2;
	ball_y = width / 2;
	a[ball_x][ball_y] = 3;
	HideCursor();
}
void show()  //显示游戏画面
{
	int i, j;
	for (i = 0; i < high; i++)
	{
		for (j = 0; j < width; j++)
		{
			if (a[i][j] == 1)
				printf("#");//边框
			else if (a[i][j] == 2)
				printf("=");//挡板=
			else if (a[i][j] == 3)
				printf("*");//小球
			else if (a[i][j] == 4)
				printf("B");//障碍物
			else
				printf(" ");
		}
		printf("\n");
	}

	gotoxy(0, 0);//清屏

}
void upstateWithoutInput()//与用户输入无关的更新
{
	static int speed = 10;
	if (speed < v)
		speed++;
	if (speed == v)
	{
		a[ball_x][ball_y] = 0;
		a[ball_x + v_x][ball_y + v_y] = 3;
		ball_x = ball_x + v_x;
		ball_y = ball_y + v_y;
		speed = 0;
		if (a[ball_x - 1][ball_y] == 4 || a[ball_x - 1][ball_y + v_y] == 4)
		{
			v_x = -v_x;
			if (a[ball_x - 1][ball_y] == 0)
				a[ball_x - 1][ball_y + v_y] = 0;
			else
				a[ball_x - 1][ball_y] = 0;
			printf("\a");
			sum++;
		}
		if (ball_x == 1)
		{
			v_x = -v_x;
		}
		if (ball_y == width - 2 || ball_y == 1)
		{
			v_y = -v_y;
		}
		if (ball_x == high - 3)
		{
			if (a[high - 2][ball_y + v_y] == 2)
			{
				v_x = -v_x;
			}
			else if (a[high - 2][ball_y + v_y] == 0)
			{
				printf("failed!");
				system("pause");
				exit(0);
			}
		}
	}
	if (sum == 5)
	{
		radius = 4;
	}
	if (sum == 10)
	{
		radius = 5;
		v = 9;
	}
	if (sum == 15)
	{
		radius = 6;
		v = 8;
	}
	if (sum == 20)
	{
		v = 7;
	}
}
void upstateWithInput()//与用户输入有关的更新
{
	char input;
	if (_kbhit())//当按下键的时候
	{
		input = _getch();
		if (input == 'a')
		{
			if (position_y - radius - 2 > 0)
			{
				a[position_x][position_y + radius] = 0;
				position_y--;
				a[position_x][position_y - radius] = 2;
			}
		}
		if (input == 'd')
		{
			if (position_y + radius + 2 < width - 1)
			{
				a[position_x][position_y - radius] = 0;
				position_y++;
				a[position_x][position_y + radius] = 2;
			}

		}
	}
}
int main()
{
	startup();   //数据初始化
	while (1)
	{
		show();  //显示游戏画面
		upstateWithoutInput();//与用户输入无关的更新
		upstateWithInput();//与用户输入有关的更新
	}
	return 0;
}
